<?php
/**
 * Index.php
 * Niushop商城系统 - 团队十年电商经验汇集巨献!
 * =========================================================
 * Copy right 2015-2025 山西牛酷信息科技有限公司, 保留所有权利。
 * ----------------------------------------------
 * 官方网址: http://www.niushop.com.cn
 * 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和使用。
 * 任何企业和个人不允许对程序代码以任何形式任何目的再发布。
 * =========================================================
 * @author : niuteam
 * @date : 2015.1.17
 * @version : v1.0.0.0
 */

namespace app\api\controller;


use app\platform\controller\Upload;
use data\extend\dir;
use data\service\EnterpriseRegistration;
use data\service\Goods as GoodsService;
use data\service\Platform;
use data\service\User;
use think\cache\driver\Redis;
use think\Controller;
use app\Validate\TestValidate;
/**
 * 后台主界面
 *
 * @author Administrator
 *
 */
class Index extends Controller
{
    /**
     * 商品列表
     *
     * @return false|string
     * @author dzl
     */
    public function getAllGoods(TestValidate $testValidate)
    {
        $page_index = request()->get('pageIndex', 1);
        $goods_name = request()->get('goods_name', '');
        $state = request()->get('state', '');
        $shop_id = request()->get('shop_id', '');
        $goodservice = new GoodsService();
        $condition = '';
        if ($state != "") {
            $condition["ng.state"] = $state;
        }

        if (!empty($goods_name)) {
            $condition["ng.goods_name"] = array(
                "like",
                "%" . $goods_name . "%"
            );
        }
        if ($shop_id != '') {
            $condition["ng.shop_id"] = $shop_id;
        }
        $result = $goodservice->getGoodsList($page_index, 20, $condition, 'ng.create_time desc');
        $platform = new Platform();
        if (!empty($result['data'])) {
            foreach ($result['data'] as $k => $v) {
                $result['data'][$k]['is_platform_new'] = $platform->getGoodsIsnew($v['goods_id']);
                $result['data'][$k]['is_platform_best'] = $platform->getGoodsIsBest($v['goods_id']);
                $result['data'][$k]['is_platform_hot'] = $platform->getGoodsIshot($v['goods_id']);
            }
        }
        return json_encode(ApiReturn(SUCCESS, $result));
    }

    public function getOneGood()
    {

    }


    /**
     * 企业入驻
     *
     * @return string
     * @throws \Exception
     * @author dzl
     */
    public function settledIn()
    {
        //存储地址
        $file_path = "upload/business/";
        //企业图片
        //营业执照
        //身份证正面
        //身份证反面

        $imagesName = 'images';
        $businessLicenceName = 'business';
        $idCardBackName = 'idCardBack';
        $idCardFrontName = 'idCardFrond';

        $upload = new Upload();
        $img = $upload->uploadGivenFile($imagesName,$file_path);
        $business = $upload->uploadGivenFile($businessLicenceName,$file_path);
        $idCardFront = $upload->uploadGivenFile($idCardFrontName,$file_path);
        $idCardBack = $upload->uploadGivenFile($idCardBackName,$file_path);

        $img = json_decode($img , true);
        $business = json_decode($business , true);
        $idCardFront = json_decode($idCardFront , true);
        $idCardBack = json_decode($idCardBack , true);
        if($img['code'] != 1 || $business['code'] != 1 ||$idCardFront['code'] != 1 ||$idCardBack['code'] != 1){
            return return_json_data(false);

        }

        $loadPath = $img['data'];
        $businessLicence = $business['data'];
        $idCardFront = $idCardFront['data'];
        $idCardBack = $idCardBack['data'];

//        if (!request()->isPost()) {
//            return json_encode(ApiReturn(IMPROPER_OPERATION));
//        }

        $name = request()->post('name', '');
        $phone = request()->post('phone', '');
        $desc = request()->post('desc', '');
        $address = request()->post('address', '');
        $details = request()->post('details', '');
        $operator = request()->post('operator', 1);
        $type = request()->post('type', '');
        $sms = request()->post('sms', '');

        $settledInType = 1;

        $article = new EnterpriseRegistration();
        $result = $article->addSettledIn($name,$phone, $desc, $address, $details, $operator,$loadPath,$businessLicence,$idCardBack,$idCardFront,$type,$settledInType,$sms);

        return return_json_data($result);
    }

    /**
     * 创客入驻
     *
     * @return string
     * @throws \Exception
     * @author dzl
     */
    public function ckSettledIn(){
        $name = request()->post('name', '');
        $phone = request()->post('phone', '');
        $article = new EnterpriseRegistration();
        $settledInType = 2;
        $result = $article->addSettledIn($name,$phone, $desc ="", $address="", $details="", $operator="",$loadPath="",$businessLicence="",$idCardBack="",$idCardFront="",$type="",$settledInType);
        return return_json_data(true);
    }

    /**
     * 驳回接口
     *
     * @return false|string
     * @authr dzl
     */
    public function reject()
    {
        $id = request()->post('id');
        $status = request()->post('status');
        $reject_info = request()->post('reject_info');
        $article = new EnterpriseRegistration();
        $result = $article->saveSettledIn($id,$status,$reject_info);
        return json_encode(ApiReturn(SUCCESS, $result));

    }

    /**
     * 修改新密码
     *
     * @return string
     * @throws \Exception
     * @author dzl
     */
    public function updatePassword(){
        $user = new User();
        $id = request()->post('id');
        $oldPassword = request()->post('old_password');
        $newPassword = request()->post('new_password');
        $res = $user->updateUserPasswordByPassword($id,$oldPassword,$newPassword);
        return return_json_data($res);
    }

    /**
     * 忘记密码
     *
     * @return string
     * @throws \Exception
     * @author dzl
     */
    public function forgetPassword(){
        $user = new User();
        $id = request()->post('id');
        $phone = request()->post('phone');
        $newPassword = request()->post('new_password');
        $sms = request()->post('sms');
        $res = $user->updateUserPasswordBySms($id,$phone,$sms,$newPassword);
        return return_json_data($res);

    }

    /**
     * 更新用户信息
     *
     * @return string
     * @throws \Exception
     * @author dzl
     */
    public function updateUserInfo(){
        $user = new User();
        $file_path = "upload/avator/";
        $imagesName = 'images';
        $upload = new Upload();
        $img = $upload->uploadGivenFile($imagesName,$file_path);

        $img = json_decode($img , true);
        if($img['code'] != 1){
            return return_json_data(false);

        }
        $loadPath = $img['data'];
        $id = request()->post('id');
        $username = request()->post('username');
        $res = $user->saveUserInfo($id,$username,$loadPath);
        return return_json_data($res);
    }

}
